package com.anjiplus.template.gaea.business.modules.tbscheduledgaeareport.service.impl;

import com.anjiplus.template.gaea.business.modules.tbscheduledgaeareport.dao.TBScheduledGaeaReportDao;
import com.anjiplus.template.gaea.business.modules.tbscheduledgaeareport.entity.TBScheduledGaeaReport;
import com.anjiplus.template.gaea.business.modules.tbscheduledgaeareport.form.TBScheduledGaeaReportForm;
import com.anjiplus.template.gaea.business.modules.tbscheduledgaeareport.service.TBScheduledGaeaReportService;
import com.anjiplus.template.gaea.business.modules.tbscheduledgaeareport.vo.TBScheduledGaeaReportVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * TBScheduledGaeaReport服务实现类
 * </p>
 *
 * @author admin
 * @since 2023-08-24
 */
@Service
public class TBScheduledGaeaReportServiceImpl extends ServiceImpl<TBScheduledGaeaReportDao, TBScheduledGaeaReport> implements TBScheduledGaeaReportService {

    @Override
    public int save(TBScheduledGaeaReportForm record) {
        TBScheduledGaeaReport data = this.convertDO(record);
        data.setCreatedTime(LocalDateTime.now());
        data.setDeleteState(0);
        int insert = baseMapper.insert(data);
        record.setId(data.getId());
        return insert;
    }

    @Override
    public int updateById(TBScheduledGaeaReportForm record) {
        //修改时间
        TBScheduledGaeaReport data = this.convertDO(record);
        data.setUpdatedTime(LocalDateTime.now());
        int update = baseMapper.updateById(data);
        record.setId(data.getId());
        return update;
    }

    @Override
    public int deleteById(String id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public TBScheduledGaeaReportVO selectById(String id) {
        TBScheduledGaeaReport data = baseMapper.selectById(id);
        TBScheduledGaeaReportVO vo = new TBScheduledGaeaReportVO();
        BeanUtils.copyProperties(data, vo);
        return vo;
    }

    @Override
    public List<TBScheduledGaeaReportVO> selectAll(TBScheduledGaeaReportForm record) {
        TBScheduledGaeaReport data = this.convertDO(record);
        QueryWrapper<TBScheduledGaeaReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(data);
        List<TBScheduledGaeaReport> list = baseMapper.selectList(queryWrapper);
        return this.convert(list);
    }

    @Override
    public IPage<TBScheduledGaeaReportVO> selectPage(TBScheduledGaeaReportForm record) {
        // form -> do 转换
        TBScheduledGaeaReport data = this.convertDO(record);
        // 分页数据设置
        Page<TBScheduledGaeaReport> page = new Page<>(record.getCurrent(), record.getSize());
        // 查询条件
        QueryWrapper<TBScheduledGaeaReport> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(data.getScheduledName())){
            queryWrapper.like("\"scheduled_name\"",data.getScheduledName());
        }
        if (!StringUtils.isEmpty(data.getEnableStatus())){
            queryWrapper.like("\"enable_status\"",data.getEnableStatus());
        }
        IPage<TBScheduledGaeaReport> list = baseMapper.selectPage(page, queryWrapper);
        IPage<TBScheduledGaeaReportVO> iPage = new Page<>();
        iPage.setRecords(this.convert(list.getRecords()));
        iPage.setCurrent(list.getCurrent());
        iPage.setSize(list.getSize());
        iPage.setTotal(list.getTotal());
        iPage.setPages(list.getPages());
        return iPage;
    }

    @Override
    public IPage<TBScheduledGaeaReport> getPage(IPage<TBScheduledGaeaReport> page, QueryWrapper<TBScheduledGaeaReport> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }


    /**
     * Form -> Do
     *
     * @param form 对象
     * @return Do对象
     */
    private TBScheduledGaeaReport convertDO(TBScheduledGaeaReportForm form) {
        TBScheduledGaeaReport data = new TBScheduledGaeaReport();
        BeanUtils.copyProperties(form, data);
        return data;
    }

    /**
     * Do -> VO
     *
     * @param list 对象
     * @return VO对象
     */
    private List<TBScheduledGaeaReportVO> convert(List<TBScheduledGaeaReport> list) {
        List<TBScheduledGaeaReportVO> tBScheduledGaeaReportList = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)) {
            return tBScheduledGaeaReportList;
        }
        for (TBScheduledGaeaReport source : list) {
            TBScheduledGaeaReportVO target = new TBScheduledGaeaReportVO();
            BeanUtils.copyProperties(source, target);
            tBScheduledGaeaReportList.add(target);
        }
        return tBScheduledGaeaReportList;
    }

}
